﻿using System;
using System.Collections.Generic;
using System.Threading;
using mmSubProvider;
using NUnit.Framework;

namespace mmTests
{
    [TestFixture]
    public class SubtitleTest
    {
        #region MEMBERS

        private readonly List<SubSearchResult> _mSubList = new List<SubSearchResult>();
        private List<SubSearchResult> _mSubResult = null;
        private readonly List<Tuple<SubSearchResult, string>> _mLinks = new List<Tuple<SubSearchResult, string>>();
        private readonly object _mLock = new object();
        private string _mSubtitleUrl = null;

        private int _mWaited = 0;

        private const int LoopWait = 1000;
        private const int MaxWait = 60000;

        #endregion MEMBERS

        #region TESTS

        [Test]
        public void SubTest1()
        {
            var ss = new SubSearch();

            ss.OnFound += ss_OnFound;
            ss.OnSearchProgress += ss_OnSearchProgress;
            if(!ss.Search("house"))
            {
                Assert.Fail("Movie not found!");
            }

            while(_mSubResult == null)
            {
                Thread.Sleep(LoopWait);
                _mWaited += LoopWait;
                if(_mWaited >= MaxWait)
                {
                    Assert.Fail("Movies searching timeout!");
                }
            }

            Assert.AreEqual(_mSubResult.Count, _mSubList.Count);

            var rr = new Random(DateTime.Now.Millisecond);

            var nCount = _mSubList.Count;

            foreach(var sresult in _mSubList)
            {
                if(sresult.SeasonCount == 0)
                {
                    nCount--;
                    continue;
                }
                var sp = new SubDownload();
                sp.OnSubtitleAvailable += sp_OnSubtitleAvailable;
                sp.SubtitleAvailable(sresult, rr.Next(1, sresult.SeasonCount), rr.Next(1, 15));
            }

            while (_mLinks.Count < nCount)
            {
                Thread.Sleep(LoopWait);
                _mWaited += LoopWait;
                if (_mWaited >= MaxWait)
                {
                    Assert.Fail("Subtitle searching timeout!");
                }
            }

            Assert.AreEqual(_mLinks.Count, nCount, "Wrong count of links!");
        }

        [Test]
        public void SubProviderTest()
        {
            var sp = new SubProvider();
            sp.OnFound += sp_OnFound;
            sp.FindSubtitle("Desperate Housewives", 4, 12);

            while (_mSubtitleUrl == null)
            {
                Thread.Sleep(LoopWait);
                _mWaited += LoopWait;
                if (_mWaited >= MaxWait)
                {
                    Assert.Fail("Subtitle searching timeout!");
                }
            }

            Assert.AreNotEqual(_mSubtitleUrl, null);
        }

        #endregion TESTS

        #region HANDLERS

        private void sp_OnFound(string url)
        {
            lock (_mLock)
            {
                _mSubtitleUrl = url;
            }
        }

        private void sp_OnSubtitleAvailable(SubSearchResult sr, string subLink)
        {
            lock(_mLock)
            {
                _mLinks.Add(new Tuple<SubSearchResult, string>(sr, subLink));
            }
        }

        private void ss_OnSearchProgress(SubSearchResult result)
        {
            _mSubList.Add(result);
        }

        private void ss_OnFound(List<SubSearchResult> result)
        {
            lock (_mLock)
            {
                _mSubResult = result;
            }
        }

        #endregion HANDLERS
    }
}
